/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Vue;

import Engine.Batiment;
import Engine.Entite;
import Engine.Unite;
import java.util.ArrayList;
import java.util.List;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.geom.Rectangle;

/**
 *
 * @author Ivelios
 */
public class Selection implements IElementGraphique{
    private Rectangle selection;
    private Vector2f origine;
    private Camera camera;
    private List<Entite> listEntiteSelected;
    private Lanceur lanceur;


    Selection(Camera camera, Lanceur lanceur) {
       this.camera = camera;
       this.lanceur = lanceur;
       this.listEntiteSelected = new ArrayList<Entite>();
    }

    public void init(GameContainer container) throws SlickException {}

    public void render(GameContainer gc, Graphics g) {
        g.resetTransform();
        if(selection != null){
            g.scale(camera.getZoom(),camera.getZoom());
            g.setLineWidth(1.0f);
            g.setColor(new Color(1,215,88));
            g.drawRect(selection.getX()+camera.getPosX(), selection.getY()+camera.getPosY(), selection.getWidth(), selection.getHeight());
            for(Entite entite : this.listEntiteSelected){
                int ecart = 3;//ecart entre l'entite et le cercle de selection
                g.setColor((new Color(1,215,88)));
                g.setLineWidth(4.0f*camera.getZoom());
                g.drawOval(camera.getPosX()+entite.getPosX()-ecart,
                        camera.getPosY()+entite.getPosY()-ecart,
                        entite.getWidth()+(2*ecart),
                        entite.getHeight()+(2*ecart));
            }
            
        }
    }

    public void update(GameContainer gc, int delta) {
       if(selection != null){
            //Redimensionne le rectangle de selection
            Vector2f newPoint = RefToMap(gc.getInput().getMouseX(),gc.getInput().getMouseY());
            if(newPoint.getX() < origine.getX()){
                selection.setX((float) newPoint.getX());
                selection.setWidth((float) (origine.getX() - newPoint.getX()));
            }else if(newPoint.getX() >= origine.getX()){
                selection.setX((float) origine.getX());
                selection.setWidth((float) (newPoint.getX() - selection.getX()));
            }

            if(newPoint.getY() < origine.getY()){
                selection.setY((float) newPoint.getY());
                selection.setHeight((float) (origine.getY() - newPoint.getY()));
            }else if(newPoint.getY()>= origine.getY()){
                selection.setY((float) origine.getY());
                selection.setHeight((float) (  newPoint.getY() - selection.getY()));
            }

            RefreshSelection();


        }
    }

    //Convertie un point de la fenetre en un point sur la carte
    private Vector2f RefToMap(int x , int y){
        int resultX = (int) ((x - (camera.getPosX() * camera.getZoom()))/camera.getZoom());
        int resultY =  (int) ((y - (camera.getPosY() * camera.getZoom()))/camera.getZoom());
        
        return new Vector2f(resultX, resultY);
    }


    public void mouseReleased(int button, int x, int y){

        //Gestion de la selection
        if(button == Input.MOUSE_LEFT_BUTTON){
            selection = null;
        }
        
    }

    public void mousePressed(int button, int x, int y){
        //Initialise le rectangle de selection
        if(button == Input.MOUSE_LEFT_BUTTON){
            System.out.println(x+" | "+y);
            Vector2f p = RefToMap(x,y);
            origine = p;
            selection = new Rectangle((int)p.getX(),(int)p.getY(),0,0);
        }
    }

    private void RefreshSelection(){
        //Supprime les unites selectionnees precedemment
        if(!listEntiteSelected.isEmpty()){
            listEntiteSelected.removeAll(listEntiteSelected);
        }
        //Ajoute les unites selectionnes a la liste de selection
        for(Unite unite : lanceur.getListUnite()){
            if(selection.contains(unite.getMiddleX(), unite.getMiddleY())){
                listEntiteSelected.add(unite);
            }
        }

        //Si aucune unite n est selectionnees, on selectionne les batiments
        if(listEntiteSelected.isEmpty()){
            for(Batiment batiment : lanceur.getListBatiment()){
                if(selection.contains(batiment.getMiddleX(), batiment.getMiddleY())){
                    listEntiteSelected.add(batiment);
                }
            }
        }
 
    }

}
